package com.mingzhi.forum.controller;

import com.mingzhi.forum.common.AppResult;
import com.mingzhi.forum.common.ResultCode;
import com.mingzhi.forum.config.AppConfig;
import com.mingzhi.forum.exception.ApplicationException;
import com.mingzhi.forum.model.Article;
import com.mingzhi.forum.model.ArticleReply;
import com.mingzhi.forum.model.User;
import com.mingzhi.forum.service.IArticleReplyService;
import com.mingzhi.forum.service.IArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "帖子回复接口API")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource
    private IArticleReplyService articleReplyService;

    @Resource
    private IArticleService articleService;

    @Operation(summary = "回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @Parameter(description = "帖子id") @RequestParam("articleId") @NonNull Long articleId,
                            @Parameter(description = "帖子回复内容") @RequestParam("content") @NonNull String content) {
        //获取用户信息
        User user = (User) request.getSession(false).getAttribute(AppConfig.USER_SESSION);
        //判断用户是否被禁言
        if (user.getState() == 1) {
            //打印日志
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            //抛出异常
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //获取帖子信息
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            //打印日志
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            //抛出异常
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if (article.getState() == 1) {
            //打印日志
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            //抛出异常
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        ArticleReply createArticleReply = new ArticleReply();
        createArticleReply.setArticleId(articleId);//帖子id
        createArticleReply.setPostUserId(user.getId());//回复的发送者
        createArticleReply.setContent(content);//回复的内容
        createArticleReply.setReplyId(null);
        createArticleReply.setReplyUserId(null);
        System.out.println(createArticleReply);

        //调用service层
        articleReplyService.create(createArticleReply);
        return AppResult.success();
    }

    @Operation(summary = "帖子列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(
            @Parameter(description = "帖子id") @RequestParam("articleId") @NonNull Long articleId) {
        //根据帖子id查询帖子是否存在
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1){
            //打印日志
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            //抛出异常
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if (article.getState() == 1) {
            //打印日志
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            //抛出异常
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        List<ArticleReply> result = articleReplyService.selectByArticleId(articleId);
        return AppResult.success(result);
    }
}
